﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;
namespace Congducsan
{
    public partial class FrmTdTcTn : Form
    {
        public FrmTdTcTn()
        {
            InitializeComponent();
        }

        private void FrmTdTcTn_FormClosing(object sender, FormClosingEventArgs e)
        {
            DradApplication.DradApp.FormMain.Show();
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmTdTcTn_Resize(object sender, EventArgs e)
        {
            this.Size = new Size(570, 542);
        }

        private void tabControl2_Click(object sender, EventArgs e)
        {
            if (tabControl2.SelectedTab == tabControl2.TabPages[0])
            {
                pictureBox1.Image = Congducsan.Properties.Resources.ds_tdstn_1;
            }
            else
            {
                if (tabControl2.SelectedTab == tabControl2.TabPages[1])
                {
                    pictureBox1.Image = Congducsan.Properties.Resources.ds_tdstn_2;
                }
                else
                {
                    pictureBox1.Image = Congducsan.Properties.Resources.ds_tdstn_3;
                }
               
            }
        }

        private void CmdOk_Click(object sender, EventArgs e)
        {
            PrTdTcTn prTdTcTn = new PrTdTcTn();
            prTdTcTn.Show();
            this.Hide();
        }

        private void FrmTdTcTn_Load(object sender, EventArgs e)
        {
            loadDataGridView();
            loadSettingReg();
        }

           //hàm nhận giá trị đưa vào datagridview
        private void SanTn1(int col, int row, string Value)
        {
            col = col - 1;
            row = row - 1;
            dgvSanTieuNang.Rows[row].Cells[col].Value = Value;
        }

        //hàm lấy giá trị từ datagridview
        private string SanTn2(int col, int row)
        {
            col = col - 1;
            row = row - 1;
            string Value = dgvSanTieuNang.Rows[row].Cells[col].Value.ToString();
            return Value;
        }

        //Hiện thị dữ liệu lên dgv
        private void loadDataGridView()
        {
            dgvSanTieuNang.Rows.Add("Dày tường T");
            dgvSanTieuNang.Rows.Add("Cao tường h");
            dgvSanTieuNang.Rows.Add("Dày sân h1");
            dgvSanTieuNang.Rows.Add("Dày móng bể h2");
            dgvSanTieuNang.Rows.Add("Dày sân h3");
            dgvSanTieuNang.Rows.Add("Sâu chân khay H1");
            dgvSanTieuNang.Rows.Add("Sâu bê H2");
            dgvSanTieuNang.Rows.Add("Sâu bê H3");
            dgvSanTieuNang.Rows.Add("Sâu chân khay H4");
            dgvSanTieuNang.Rows.Add("Dài sân L");
            dgvSanTieuNang.Rows.Add("Dài bể L1");
            dgvSanTieuNang.Rows.Add("Dài sân L2");
            dgvSanTieuNang.Rows.Add("Dày chân khay b");
            dgvSanTieuNang.Rows.Add("Dày chân khay b1");
            dgvSanTieuNang.Rows.Add("Dày thành bể b2");
            dgvSanTieuNang.Rows.Add("Vát thành bể a");
            dgvSanTieuNang.Rows.Add("Rộng gờ d");
            dgvSanTieuNang.Rows.Add("Cách khoảng gờ d1");
        }

        // Lưu settings vào registry
        private void saveSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            string Values = "";
            //Tuong dau
            Values = txtTDau_DayMTr .Text;//0
            Values = Values +"\t"+ txtTDau_DayMSau .Text;//1
            Values = Values +"\t"+ txtTDau_Day .Text;//2
            Values = Values +"\t"+ txtTDau_doc.Text;//3
            Values = Values +"\t"+ txtTDau_mrongT .Text;//4
            Values = Values +"\t"+ txtTDau_mrongS .Text;//5
            //Tuong canh
            Values = Values +"\t"+ TxtTcCaoH2 .Text;//6
            Values = Values +"\t"+ TxtTcDaySan .Text;//7
            Values = Values +"\t"+ TxtTcSauCk .Text;//8
            Values = Values +"\t"+ txtTcMrongtuong .Text;//9
            Values = Values +"\t"+ TxtTcDayCk .Text;//10
            Values = Values +"\t"+ TxtTcGoca1 .Text;//11
            Values = Values +"\t"+ TxtTcMorongm2 .Text;//12
            Values = Values +"\t"+ txtMorongsan .Text;//13
            Values = Values +"\t"+ txtLopdem .Text;//14
            Values = Values +"\t"+ TxtTcDaytuong .Text;//15
            Values = Values +"\t"+ "a" ;//16
            Values = Values +"\t"+ txtCaotaluyh1  .Text;//17
            Values = Values +"\t"+ txtTDau_cao .Text;//18
            Values = Values +"\t"+ txtTDau_mrongB .Text;//19
            //San tieu nang
            //Du lieu bo sung cho phien ban 2011
            Values = Values +"\t"+ txtSophandoan.Text ;//20
            Values = Values +"\t"+ SanTn2(2, 1) ;//21
            Values = Values +"\t"+ SanTn2(2, 2) ;//22
            Values = Values +"\t"+ SanTn2(2, 3) ;//23
            Values = Values +"\t"+ SanTn2(2, 4) ;//24
            Values = Values +"\t"+ SanTn2(2, 5) ;//25
            Values = Values +"\t"+ SanTn2(2, 6) ;//26
            Values = Values +"\t"+ SanTn2(2, 7) ;//27
            Values = Values +"\t"+ SanTn2(2, 8) ;//28
            Values = Values +"\t"+ SanTn2(2, 9) ;//29
            Values = Values +"\t"+ SanTn2(2, 10) ;//30
            Values = Values +"\t"+ SanTn2(2, 11) ;//31
            Values = Values +"\t"+ SanTn2(2, 12) ;//32
            Values = Values +"\t"+ SanTn2(2, 13) ;//33
            Values = Values +"\t"+ SanTn2(2, 14) ;//34
            Values = Values +"\t"+ SanTn2(2, 15) ;//35
            Values = Values +"\t"+ SanTn2(2, 16) ;//36
            Values = Values +"\t"+ SanTn2(2, 17) ;//37
            Values = Values +"\t"+ SanTn2(2, 18) ;//38
            //Tuong dau 5/2011
            Values = Values +"\t"+ txtTDau_Rongmong .Text;//39
            Values = Values +"\t"+ opt_b3.Checked.ToString();//40
            Values = Values +"\t"+ opt_b.Checked.ToString();//41

            string Values1 = "";
            Values1 = txtRongroda .Text;//1
            Values1 = Values1 +"\t"+ txtCaoroda .Text;//2
            Values1 = Values1 +"\t"+ txtDairoda.Text;//3
            Values1 = Values1 +"\t"+ 1;//4
            Values1 = Values1 +"\t"+ ChcRd.Checked.ToString();//5

            regKey.SetValue("KtTdTcTn", Values);
            regKey.SetValue("KtStn", Values1);
            regKey.Close();
        }

          // Load settings từ registry vào form
        private void loadSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            RegistryKey testKey = regKey.OpenSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            if (testKey != null)
            {
                regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
                string str = regKey.GetValue("KtTdTcTn").ToString();
                string[] STR = str.Split('\t');
                int i = 0;
                    //Tuong dau
                txtTDau_DayMTr .Text=STR[i++];//0
                txtTDau_DayMSau.Text = STR[i++];//1
                txtTDau_Day.Text = STR[i++];//2
                txtTDau_doc.Text = STR[i++];//3
                txtTDau_mrongT.Text = STR[i++];//4
                txtTDau_mrongS.Text = STR[i++];//5
                //Tuong canh
                TxtTcCaoH2.Text = STR[i++];//6
                TxtTcDaySan.Text = STR[i++];//7
                TxtTcSauCk.Text = STR[i++];//8
                txtTcMrongtuong.Text = STR[i++];//9
                TxtTcDayCk.Text = STR[i++];//10
                TxtTcGoca1.Text = STR[i++];//11
                TxtTcMorongm2.Text = STR[i++];//12
                txtMorongsan.Text = STR[i++];//13
                txtLopdem.Text = STR[i++];//14
                TxtTcDaytuong.Text = STR[i++];//15
                //"a" .Text;//16
                i++;
                txtCaotaluyh1.Text = STR[i++];//17
                txtTDau_cao.Text = STR[i++];//18
                txtTDau_mrongB.Text = STR[i++];//19
                //San tieu nang
                //Du lieu bo sung cho phien ban 2011
                txtSophandoan.Text = STR[i++];//20
                SanTn1(2, 1, STR[i++]);//21
                SanTn1(2, 2, STR[i++]);//22
                SanTn1(2, 3, STR[i++]);//23
                SanTn1(2, 4, STR[i++]);//24
                SanTn1(2, 5, STR[i++]);//25
                SanTn1(2, 6, STR[i++]);//26
                SanTn1(2, 7, STR[i++]);//27
                SanTn1(2, 8, STR[i++]);//28
                SanTn1(2, 9, STR[i++]);//29
                SanTn1(2, 10, STR[i++]);//30
                SanTn1(2, 11, STR[i++]);//31
                SanTn1(2, 12, STR[i++]);//32
                SanTn1(2, 13, STR[i++]);//33
                SanTn1(2, 14, STR[i++]);//34
                SanTn1(2, 15, STR[i++]);//35
                SanTn1(2, 16, STR[i++]);//36
                SanTn1(2, 17, STR[i++]);//37
                SanTn1(2, 18, STR[i++]);//38
                //Tuong dau 5/2011

                txtTDau_Rongmong.Text = STR[i++];//39
                opt_b3.Checked = bool.Parse(STR[i++]);//40
                opt_b.Checked = bool.Parse(STR[i++]);//41

                string[] str1 = regKey.GetValue("KtStn").ToString().Split('\t');
                txtRongroda .Text=str1[0];//1
                txtCaoroda.Text = str1[1];//2
                txtDairoda.Text = str1[2];//3
                //1 .Text;//4
                ChcRd.Checked = bool.Parse(str1[4]);//5
                regKey.Close();
            }
        }

         // Save setting vào file
        private void saveSettingFile()
        {
            SaveFileDialog svDialog = new SaveFileDialog();
            svDialog.Filter = "Data files (*.stn)|*.stn";
            svDialog.InitialDirectory = "C://";
            svDialog.RestoreDirectory = true;
            string filename = "";
            if (svDialog.ShowDialog() == DialogResult.OK)
            {
                filename = svDialog.FileName;
                string Str;
                //Tuong dau
                Str = "------TUONG DAU------" +"\r\n";
                Str = Str + "Cao dinh tuong H1:" +"\t"+ txtTDau_cao .Text+"\r\n";
                Str = Str + "Day mong truoc H2:" +"\t"+ txtTDau_DayMTr .Text+"\r\n";
                Str = Str + "Day mong sau H3:" +"\t"+ txtTDau_DayMSau .Text+"\r\n";
                Str = Str + "Lop dem H4:" +"\t"+ txtLopdem .Text+"\r\n";
                Str = Str + "Cao dinh taluy H5:" +"\t"+ txtCaotaluyh1 .Text+"\r\n";
                Str = Str + "Day dinh tuong b1:" +"\t"+ txtTDau_Day .Text+"\r\n";
                Str = Str + "Mo mong truoc b2:" +"\t"+ txtTDau_mrongT .Text+"\r\n";
                Str = Str + "Mo mong sau b3:" +"\t"+ txtTDau_mrongS .Text+"\r\n";
                Str = Str + "Rong mong b:" +"\t"+ txtTDau_Rongmong .Text+"\r\n";
                Str = Str + "Gan theo mo rong:" +"\t"+ opt_b3.Checked.ToString()+"\r\n";
                Str = Str + "Gan theo rong mong:" +"\t"+ opt_b.Checked.ToString()+"\r\n";
                Str = Str + "Doc lung tuong i:" +"\t"+ txtTDau_doc .Text+"\r\n";
                Str = Str + "Rong dinh taluy b4:" +"\t"+ txtRongtaluyB1 .Text+"\r\n";
                Str = Str + "Mo rong san b5:" +"\t"+ txtMorongsan .Text+"\r\n";
                //Tuong canh
                Str = Str + "------TUONG CANH------" +"\r\n";
                Str = Str + "Cao tuong H1:" +"\t"+ TxtTcCaoH1 .Text+"\r\n";
                Str = Str + "Cao tuong H2:" +"\t"+ TxtTcCaoH2 .Text+"\r\n";
                Str = Str + "Sau chan khay H3:" +"\t"+ TxtTcSauCk .Text+"\r\n";
                Str = Str + "Day san cong H4:" +"\t"+ TxtTcDaySan .Text+"\r\n";
                Str = Str + "Mo rong dinh b1:" +"\t"+ txtTcMrongtuong .Text+"\r\n";
                Str = Str + "Day chan khay b2:" +"\t"+ TxtTcDayCk .Text+"\r\n";
                Str = Str + "Day dinh tuong b3:" +"\t"+ TxtTcDaytuong .Text+"\r\n";
                Str = Str + "Mo rong mong b4:" +"\t"+ txtTDau_mrongB .Text+"\r\n";
                Str = Str + "Mo rong mong b5:" +"\t"+ TxtTcMorongm2 .Text+"\r\n";
                Str = Str + "Goc mo tren:" +"\t"+ TxtTcGoca1 .Text+"\r\n";
                Str = Str + "Goc mo duoi:" +"\t"+ TxtTcGoca2 .Text+"\r\n";
                Str = Str + "Doc taluy tuong:" +"\t"+ txtTcDocm .Text+"\r\n";
                //San tieu nang
                Str = Str + "----SAN TIEU NANG------" +"\r\n";
                Str = Str + "So phan doan:" +"\t"+ txtSophandoan .Text+"\r\n";
                Str = Str + "Day tuong T:" + "\t" + SanTn2(2, 1) + "\r\n";
                Str = Str + "Cao tuong H:" + "\t" + SanTn2(2, 2) + "\r\n";
                Str = Str + "Day san h1:" + "\t" + SanTn2(2, 3) + "\r\n";
                Str = Str + "Day mong be h2:" + "\t" + SanTn2(2, 4) + "\r\n";
                Str = Str + "Day san h3:" + "\t" + SanTn2(2, 5) + "\r\n";
                Str = Str + "Sau chan khay H1:" + "\t" + SanTn2(2, 6) + "\r\n";
                Str = Str + "Sau be H2:" + "\t" + SanTn2(2, 7) + "\r\n";
                Str = Str + "Sau be H3:" + "\t" + SanTn2(2, 8) + "\r\n";
                Str = Str + "Sau chan khay H4:" + "\t" + SanTn2(2, 9) + "\r\n";
                Str = Str + "Dai san L:" + "\t" + SanTn2(2, 10) + "\r\n";
                Str = Str + "Dai be L1:" + "\t" + SanTn2(2, 10) + "\r\n";
                Str = Str + "Dai san L2:" + "\t" + SanTn2(2, 12) + "\r\n";
                Str = Str + "Day chan khay b:" + "\t" + SanTn2(2, 13) + "\r\n";
                Str = Str + "Day chan khay b1:" + "\t" + SanTn2(2, 14) + "\r\n";
                Str = Str + "Day thanh be b2:" + "\t" + SanTn2(2, 15) + "\r\n";
                Str = Str + "Vat thanh be a" + "\t" + SanTn2(2, 16) + "\r\n";
                Str = Str + "Rong go d" + "\t" + SanTn2(2, 17) + "\r\n";
                Str = Str + "Cach khoang go d1" + "\t" + SanTn2(2, 18) + "\r\n";
                //Ghi file dữ liệu
                FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(Str);
                sw.Close();
                fs.Close();
                MessageBox.Show("OK", "Thông báo");
            }
        }
      
          // load setting từ file vào form
        private void loadSettingFile()
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Filter = "Data files (*.stn)|*.stn";
            openDialog.InitialDirectory = @"C:\";
            openDialog.FilterIndex = 2;
            openDialog.RestoreDirectory = true;
            openDialog.Multiselect = false;
            string filename;
            if (openDialog.ShowDialog() == DialogResult.OK)
            {
                filename = openDialog.FileName;
                FileStream fs = new FileStream(filename, FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string str = sr.ReadToEnd();
                fs.Close();
                fs.Close();
                string[] Values = str.Split('\n');
                int i = 1;
                //Tuong dau
                txtTDau_cao .Text=Values[i].Split('\t')[1]; i++;
                txtTDau_DayMTr .Text=Values[i].Split('\t')[1]; i++;
                txtTDau_DayMSau .Text=Values[i].Split('\t')[1]; i++;
                txtLopdem .Text=Values[i].Split('\t')[1]; i++;
                txtCaotaluyh1 .Text=Values[i].Split('\t')[1]; i++;
                txtTDau_Day .Text=Values[i].Split('\t')[1]; i++;
                txtTDau_mrongT .Text=Values[i].Split('\t')[1]; i++;
                txtTDau_mrongS .Text=Values[i].Split('\t')[1]; i++;
                txtTDau_Rongmong .Text=Values[i].Split('\t')[1]; i++;
                opt_b3.Checked=bool.Parse(Values[i].Split('\t')[1]); i++;
                opt_b.Checked = bool.Parse(Values[i].Split('\t')[1]); i++;
                txtTDau_doc .Text=Values[i].Split('\t')[1]; i++;
                txtRongtaluyB1 .Text=Values[i].Split('\t')[1]; i++;
                txtMorongsan .Text=Values[i].Split('\t')[1]; i++;
                //Tuong canh
                i = i + 1;
                TxtTcCaoH1 .Text=Values[i].Split('\t')[1]; i++;
                TxtTcCaoH2 .Text=Values[i].Split('\t')[1]; i++;
                TxtTcSauCk .Text=Values[i].Split('\t')[1]; i++;
                TxtTcDaySan .Text=Values[i].Split('\t')[1]; i++;
                txtTcMrongtuong .Text=Values[i].Split('\t')[1]; i++;
                TxtTcDayCk .Text=Values[i].Split('\t')[1]; i++;
                TxtTcDaytuong .Text=Values[i].Split('\t')[1]; i++;
                txtTDau_mrongB .Text=Values[i].Split('\t')[1]; i++;
                TxtTcMorongm2 .Text=Values[i].Split('\t')[1]; i++;
                TxtTcGoca1 .Text=Values[i].Split('\t')[1]; i++;
                TxtTcGoca2 .Text=Values[i].Split('\t')[1]; i++;
                txtTcDocm .Text=Values[i].Split('\t')[1]; i++;
                //San tieu nang
                i = i + 1;
                txtSophandoan .Text=Values[i].Split('\t')[1]; i++;
                SanTn1(2, 1,Values[i].Split('\t')[1]); i++;
                SanTn1(2, 2,Values[i].Split('\t')[1]); i++;
                SanTn1(2, 3,Values[i].Split('\t')[1]); i++;
                SanTn1(2, 4,Values[i].Split('\t')[1]); i++;
                SanTn1(2, 5,Values[i].Split('\t')[1]); i++;
                SanTn1(2, 6,Values[i].Split('\t')[1]); i++;
                SanTn1(2, 7,Values[i].Split('\t')[1]); i++;
                SanTn1(2, 8,Values[i].Split('\t')[1]); i++;
                SanTn1(2, 9,Values[i].Split('\t')[1]); i++;
                SanTn1(2, 10,Values[i].Split('\t')[1]); i++;
                SanTn1(2, 11,Values[i].Split('\t')[1]); i++;
                SanTn1(2, 12,Values[i].Split('\t')[1]); i++;
                SanTn1(2, 13,Values[i].Split('\t')[1]); i++;
                SanTn1(2, 14,Values[i].Split('\t')[1]); i++;
                SanTn1(2, 15,Values[i].Split('\t')[1]); i++;
                SanTn1(2, 16,Values[i].Split('\t')[1]); i++;
                SanTn1(2, 17,Values[i].Split('\t')[1]); i++;
                SanTn1(2, 18,Values[i].Split('\t')[1]); i++;
                MessageBox.Show("OK","Thông báo");
            }
        }

        private void cmdThietlap_Click(object sender, EventArgs e)
        {
            FrmTuychon frmTuychon = new FrmTuychon();
            frmTuychon.Show();
        }

        private void SaveData_Click(object sender, EventArgs e)
        {
            saveSettingFile();
        }

        private void LoadData_Click(object sender, EventArgs e)
        {
            loadSettingFile();
        }

        private void FrmTdTcTn_FormClosed(object sender, FormClosedEventArgs e)
        {
            saveSettingReg();
        }
    }
}
